#ifndef INIT_HPP
#define INIT_HPP

#include <iostream>

#include <string>

#include <fstream>

#include <unordered_map>

#include "log.hpp"

const std::string dictname = "./dict.txt";
const std::string sep = ":";
Log Lg;

static bool Split(const std::string &line, std::string *part1, std::string *part2)
{

    auto pos = line.find(sep);
    if (pos == std::string::npos)
    {
        return false;
    }
    *part1 = line.substr(0, pos);
    *part2 = line.substr(pos + 1);
    return true;
}

class Init
{
public:
    Init()
    {
        std::ifstream in(dictname.c_str());
        if (!in.is_open())
        {
            Lg(FATAL, "ifstream open fail");
            exit(2);
        }
        std::string line;
        while (std::getline(in, line))
        {
            std::string part1, part2;
            Split(line, &part1, &part2);
            dict.insert({part1, part2});
        }

        in.close();
    }

    const std::string translation (const std::string &key){
        auto iter = dict.find(key);
        if(iter==dict.end()){
            return "Unknow";
        }
        return iter->second;
    }

private:
    std::unordered_map<std::string, std::string> dict;
};

#endif